//
//  TFAndBtnTableViewCell.m
//  IntelligentHousehold
//
//  Created by wmwMAC on 17/6/26.
//  Copyright © 2017年 Cassie. All rights reserved.
//

#import "TFAndBtnTableViewCell.h"
#import "AddPopView.h"

#import <MessageUI/MessageUI.h>//系统短信


@interface TFAndBtnTableViewCell()<AddPopViewDelegate,MFMessageComposeViewControllerDelegate>
{
    NSString *changeTelStr;
    
    
    
    NSString *msgTitle;//短信内容
    
    NSString *NewTelStr;
    NSString *NewTleId;
}
@end

@implementation TFAndBtnTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.rightBtn.backgroundColor = RGBColor(106, 9, 189);
    
    _textLable.layer.borderWidth = 1;
    _textLable.layer.borderColor = [RGBColor(248, 248, 248) CGColor];
    _textLable.layer.cornerRadius = 5;
    _textLable.clipsToBounds = YES;
    
}
- (IBAction)RightBtnClick:(UIButton *)sender {
    
    AddPopView *addPopV = [[AddPopView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight)];
    
    UIViewController *VC =[BasePublicClass getCurrentVC];
    addPopV.dataArr = self.dataArr;
//    addPopV.type = _typeStr;
    
    
    if (self.managerDic.count) {// 更新管理员
         if (self.ManagerTypeStr == 2){//游客
            addPopV.telStr = @"";
            addPopV.telId = self.managerDic[@"id"];

        }else{//成员 或管理员
            
            addPopV.telStr = self.managerDic[@"wtell"];
            addPopV.telId = self.managerDic[@"id"];
            
        }
    }else{//更新成员
        
        addPopV.telStr = _modle.wtell;
        addPopV.telId = _modle.iid;
    
    }
    
    addPopV.delegate = self;
    
    [VC.view.window addSubview:addPopV];
}
-(void)addPopViewPassTel:(NSString *)telStr TelId:(NSString *)telId{
    
    
    // 发送短信
    if (_type == 0 ) {//更新管理元
        msgTitle = [NSString stringWithFormat:@"setphone:1,%@",telStr];
    }else{//更新成员
        NSLog(@"%ld",self.tag);

        msgTitle = [NSString stringWithFormat:@"setphone:%ld,%@",self.tag-1998,telStr];
        NSLog(@"%@",msgTitle);
    }
    
    NewTleId = telId;
    NewTelStr = telStr;
    [self sendMsg];
}

#pragma mark -------------- 发送短信 ----------------
-(void)sendMsg{
    
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSString *telStr = saveDic[@"locknumber"];
//    telStr = @"10086";
    
    [self showMessageView:[NSArray arrayWithObjects:telStr, nil] title:@"" body:msgTitle];
    
}
-(void)showMessageView:(NSArray *)phones title:(NSString *)title body:(NSString *)body
{
    if( [MFMessageComposeViewController canSendText] )
    {
        MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
        controller.recipients = phones;
        controller.navigationBar.tintColor = [UIColor redColor];
        controller.body = body;
        controller.messageComposeDelegate = self;
        
        UIViewController *VC = [BasePublicClass getCurrentVC];
        [VC presentViewController:controller animated:YES completion:nil];
        
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"该设备不支持短信功能 " delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
#pragma mark - MFMessageComposeViewController

-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    UIViewController *VC = [BasePublicClass getCurrentVC];
    [VC dismissViewControllerAnimated:YES completion:nil];
    
    if (result) {
        if (MessageComposeResultSent) {//短信发送成功
            [self EditWhiteList:NewTleId TelStr:NewTelStr];
        }
        
    }
    
}

// 编辑白名单 (添加修改管理员后要重新获取type值)
-(void)EditWhiteList:(NSString *)telId TelStr:(NSString *)telStr {
    
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSString *lockNumberStr = saveDic[@"locknumber"];
    NSString *url = [NSString stringWithFormat:EDITWHITE,RealmName];
    NSMutableDictionary *parameDic = [NSMutableDictionary dictionary];
    [parameDic setObject:saveDic[@"user_id"] forKey:@"user_id"];
    [parameDic setObject:saveDic[@"token"] forKey:@"token"];
    [parameDic setObject:lockNumberStr forKey:@"locknumber"];
    
    [parameDic setObject:telStr forKey:@"wtell"];
    
    if (_type == 0 ) {//添加修改管理员
        [parameDic setObject:@"0" forKey:@"type"];//0代表管理员 1代表成员
        NSString *telStrD = self.managerDic[@"wtell"];
        if (!telStrD.length) {//添加管理员
            [parameDic setObject:@"0" forKey:@"types"];//0代表添加 1代表修改 2代表删除
        }else{//修改管理员
        [parameDic setObject:@"1" forKey:@"types"];//0代表添加 1代表修改 2代表删除
        }
        
    }else{//修改成员
        [parameDic setObject:[NSString stringWithFormat:@"%ld",_ManagerTypeStr] forKey:@"type"];//0代表管理员 1代表成员
        [parameDic setObject:@"1" forKey:@"types"];//0代表添加 1代表修改 2代表删除
    }
    
    if (telId.length) {
        [parameDic setObject:telId forKey:@"id"];//修改删除需要这个代表白名单成员对应
    }
    
    
    
    [HttpRequest postWithURLString:url parameters:parameDic success:^(id responseObject) {
        
        NSLog(@"%@",responseObject);
        
        if ([[responseObject objectForKey:@"msg"] isEqualToString:@"success"]) {
            
//            if (_typeStr == 0) {
                [self mianLogin];
//            }
            

        }else{
            [MBProgressHUD showText:[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"msg"]]];
        }
        
        
    } failure:^(NSError *error) {
        
    }];
    
}

-(void)mianLogin{
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    
    NSString *url = [NSString stringWithFormat:MIANLOGIN,RealmName];
    NSMutableDictionary *parameDic = [NSMutableDictionary dictionary];
    [parameDic setObject:saveDic[@"tell"] forKey:@"tell"];
    
    [HttpRequest postWithURLString:url parameters:parameDic success:^(id responseObject) {
        
        [SaveDataMethord cleanloginData];//清除登录记录
        
        NSDictionary *dataDic = [NSDictionary dictionaryWithDictionary:responseObject];
        //存储信息
        [SaveDataMethord SaveLoginDataToLocationWithDic:dataDic.mutableCopy];
        
        //代理
        if ([self.delegate respondsToSelector:@selector(TFAndBtnTableViewCellPaddReloadDate:)]) {
            [self.delegate TFAndBtnTableViewCellPaddReloadDate:NewTelStr];
        }
        
        
        
    } failure:^(NSError *error) {
        
    }];
    
}





- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
